package com.zqxq.lottery.service.sys.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zqxq.common.utils.DateUtils;
import com.zqxq.common.utils.DispatcherUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.po.order.UserOrderPO;
import com.zqxq.lottery.entity.po.sys.SysAnalyzePO;
import com.zqxq.lottery.entity.po.user.UserFundsPO;
import com.zqxq.lottery.entity.vo.sys.ChannelDataExportVo;
import com.zqxq.lottery.mapper.order.UserOrderMapper;
import com.zqxq.lottery.mapper.sys.SysAnalyzeMapper;
import com.zqxq.lottery.mapper.user.UserFundsMapper;
import com.zqxq.lottery.service.order.IUserOrderService;
import com.zqxq.lottery.service.sys.ISysAnalyzeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 苦智
 * @date 2018/4/9 14:34
 * @description
 * @ClassName SysAnalyzeServiceImpl
 */
@Service
@Slf4j
public class SysAnalyzeServiceImpl extends ServiceImpl<SysAnalyzeMapper,SysAnalyzePO> implements ISysAnalyzeService{

    @Autowired
    private UserFundsMapper userFundsMapper;

    @Autowired
    private UserOrderMapper userOrderMapper;

    @Override
    public Map<String, Object> getTotalCount(String startTime, String endTime) {
        return baseMapper.getTotalCount(startTime,endTime);
    }

    @Override
    public List<Map<String, Object>> getKLineCount(String startTime,String endTime) {
        List<String> date = baseMapper.getAllDate(startTime,endTime);
        List<Map<String,Object>> resultList = new LinkedList();
        for (String d : date) {
            Map<String, Object> map = baseMapper.getKLineDataByDate(d);
            if (map != null){
                map.put("date",d);
                resultList.add(map);
            }
        }
        return resultList;
    }

    @Override
    public Map<String, Object> getEachCount() {
        Map<String,Object> eachMap= baseMapper.getEachCount();

//        Map<String,Object> orderMap = baseMapper.getOrderCount();
//        Map<String,Object> orderPrizeMap = baseMapper.getOrderPrizeCount();
//        List<Map<String,Object>> list = baseMapper.getUserCashCount();
//        Map<String,Object> cashMap = new HashMap();
//        //充值统计
//        cashMap.put("todayRechargeAmt",0);
//        cashMap.put("mouthRechargeAmt",0);
//        cashMap.put("weekRechargeAmt",0);
//        //提现统计
//        cashMap.put("todayWithdrawalsAmt",0);
//        cashMap.put("mouthWithdrawalsAmt",0);
//        cashMap.put("weekWithdrawalsAmt",0);
//        if (list.size()>=1){
//            for (Map<String, Object> map : list) {
//                if (map != null && map.get("cashType") != null){
//                    switch (Integer.parseInt(map.get("cashType").toString())){
//                        case 1:
//                            //充值统计
//                            cashMap.put("todayRechargeAmt",map.get("todayAmt"));
//                            cashMap.put("mouthRechargeAmt",map.get("mouthAmt"));
//                            cashMap.put("weekRechargeAmt",map.get("weekAmt"));
//                            break;
//                        case 2:
//                            //提现统计
//                            cashMap.put("todayWithdrawalsAmt",map.get("todayAmt"));
//                            cashMap.put("mouthWithdrawalsAmt",map.get("mouthAmt"));
//                            cashMap.put("weekWithdrawalsAmt",map.get("weekAmt"));
//                    }
//                }
//            }
//        }
//        //封装
//        orderMap.putAll(cashMap);
//        orderMap.putAll(orderPrizeMap);
        return eachMap;
    }

    @Override
    public Map<String,Object> getChannelCount(Map<String, Object> paramMap) {
        Page<Map<String,Object>> page = new Page(DispatcherUtils.parseInt(paramMap.get("pageNo")),DispatcherUtils.parseInt(paramMap.get("pageSize")));
        List<Map<String,Object>> resultList = baseMapper.getChannelCount(page,paramMap);
        page.setRecords(resultList);
        Map<String,Object> totalMap = baseMapper.getChannelTotal(paramMap);
        totalMap.put("page",page);
        return totalMap;
    }

    @Override
    public List<String> getAllChannel() {
        return baseMapper.getAllChannel();
    }

    @Override
    public Map<String, Object> getMerchantTotalCount() {
        return baseMapper.getMerchantTotalCount();
    }

    @Override
    public List<Map<String, Object>> getMerchantUserOrder(String startTime, String endTime,Long lotteryId,Long status,Page<Map<String,Object>> page) {
        List<Map<String, Object>> mapList = baseMapper.getMerchantUserOrder(startTime,endTime,lotteryId,status,page);
        for(Map<String, Object> map : mapList){
            Long mapLotteryId = (Long) map.get("lotteryId");
            String mapOrderId = (String) map.get("orderId");
            List<Map<String, Object>> betMap = baseMapper.getMerchantUserOrderBet(mapLotteryId,mapOrderId);
            map.put("betMap",betMap);
        }
        return mapList;
    }
    @Autowired
    private IUserOrderService userOrderService;
    @Override
    public void merchchantConfirmTicketOrCancelOrder(Long orderId,int status) {
        switch(status) {
            case 0:
                //确认出票
                baseMapper.merchchantConfirmTicket(orderId);
                //维护用户消费状态
                try {
                    UserOrderPO userOrderPO = userOrderMapper.selectById(orderId);
                    if (userOrderPO != null && userOrderPO.getUser_id() != null) {
                        UserFundsPO userFundsPO = new UserFundsPO();
                        userFundsPO.setUser_id(userFundsPO.getUser_id());
                        userFundsPO.setConsume_status(1);
                        userFundsMapper.updateById(userFundsPO);
                    }
                } catch (Exception e){
                    log.info("维护用户消费状态失败 系统错误");
                }
                break;
            case 1:
                //取消
                try {
                    UserOrderPO po = userOrderService.selectById(orderId);
                    userOrderService.updateOrderCancel(po);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public List<Map<String, Object>> getWinPrizeData(Map<String, Object> paramMap) {
        List<Map<String, Object>> dataList = baseMapper.getWinPrizeData(paramMap);
        return dataList;
    }

    @Override
    public Map<String, Object> getAgentDataStatistics(Map<String, Object> paramMap) {
        Page<Map<String,Object>> page = new Page(DispatcherUtils.parseInt(paramMap.get("pageNo")),DispatcherUtils.parseInt(paramMap.get("pageSize")));
        List<Map<String, Object>> dataList = baseMapper.getAgentDataStatistics(page,paramMap);
        page.setRecords(dataList);
        Map<String,Object> dataMap = baseMapper.getChannelTotalCount();
        dataMap.put("page",page);
        return dataMap;
    }

    @Override
    public List<ChannelDataExportVo> getChannelCountList(Map<String, Object> paramMap) {
        return baseMapper.getchannelCountList(paramMap);
    }
    @Override
    public List<Map<String, Object>> getDataProfitCount(String startTime,String endTime,Integer timeType) {
        List<String> dateList = baseMapper.getAllCashDate(startTime,endTime,timeType);
        List<Map<String,Object>> list = new LinkedList();
        for (String d : dateList) {
            Map<String,Object> map = baseMapper.getCashCountByDate(d,timeType);
            map.put("date",d);
            list.add(map);
        }
        return  list;
    }

    @Override
    public Map<String, Object> getTotalProfitCount(String startTime, String endTime) {
        return baseMapper.getTotalProfitCount(startTime,endTime);
    }

    @Override
    public Map<String,Object> getSaleFunnelCountTotal(String startTime, String endTime, Integer client) {
        return baseMapper.saleFunnelCountTotal(startTime,endTime,client);
    }

    @Override
    public List<Map<String, Object>> getSaleFunnelCountList(Page page, String startTime, String endTime, Integer timeType, Integer client) {
        List<Map<String,Object>> list = new LinkedList();
        List<String> pageDate = baseMapper.pageDate(page, startTime, endTime, timeType);
        for (String d : pageDate) {
            Map<String,Object> map = baseMapper.getSaleFunnelByDate(d,client,timeType);
            map.put("date",d);
            list.add(map);
        }
//        List<Map<String, Object>> list = baseMapper.getSaleFunnelCountList(page, startTime, endTime, timeType, client);
//        //按日查询复购人数单独统计
//        if (timeType == 2) {
//            for (Map<String, Object> map : list) {
//                if (map.get("date") != null){
//                    String date = map.get("date").toString();
//                    Integer reOrderUser = baseMapper.getReOrderUserByDate(date);
//                    map.put("reOrderUser",reOrderUser);
//                }
//            }
//        }
        return list;
    }

    @Override
    public Map<String, Object> getUserActivityAnalysis(String startTime, String endTime, Integer source, Integer pageNo, Integer pageSize,Integer timeType) {
        Map<String,Object> resultMap = new HashMap();
        List<Map<String,Object>> kLine = new LinkedList();
        Map<String, Object> map = baseMapper.getUserActivityAnalysis(startTime, endTime);
        String kDate = DateUtils.getCurrentDateStr();
        if (StrUtils.isNotEmpty(startTime) && StrUtils.isNotEmpty(endTime) && startTime.equals(endTime)){
            kDate = startTime;
        }
        List<String> allDate = baseMapper.getUserActivityAllDate(kDate, kDate);
        for (String d : allDate) {
            Map<String, Object> kMap = baseMapper.getUserActivityKLine(d,source);
            if (kMap != null){
                kMap.put("date",d + ":00:00");
                kLine.add(kMap);
            }
        }
        //列表
        List<Map<String,Object>> list = new LinkedList();
        Page page = new Page(pageNo,pageSize);
        List<String> pList = baseMapper.pageDate(page,startTime,endTime,timeType);
        for (String d : pList) {
            Map<String,Object> lMap = baseMapper.getUserActivityPage(d,timeType,source);
            if (lMap != null){
                lMap.put("date",d);
                list.add(lMap);
            }
        }
        page.setRecords(list);
        resultMap.put("totalMap",map);
        resultMap.put("kLine",kLine);
        resultMap.put("list",page);
        return resultMap;
    }

    @Override
    public Map<String, Object> productPerformance(String startTime,String endTime,Integer gameType) {
        Map<String, Object> dataMap = new HashMap();
        List<Map<String, Object>> list = new ArrayList();
        List<Map<String, Object>> dateList = new ArrayList();
        if(gameType==0){
            //全部
            final BigDecimal allWinRate = baseMapper.productPerformanceTotal(startTime,endTime,gameType);
            list.add(new HashMap<String,Object>(){{put("type","0");put("winRate",allWinRate);}});
            final BigDecimal winRate21 = baseMapper.productPerformanceTotal(startTime,endTime,21);
            list.add(new HashMap<String,Object>(){{put("type","21");put("winRate",winRate21);}});
            final BigDecimal winRate24 = baseMapper.productPerformanceTotal(startTime,endTime,24);
            list.add(new HashMap<String,Object>(){{put("type","24");put("winRate",winRate24);}});
            List<Map<String, Object>> listMap = baseMapper.productPerformanceTotal10(startTime,endTime,gameType);
            list.addAll(listMap);
            List<Map<String, Object>> dateGroupList = baseMapper.getDataRepresent(startTime,endTime,gameType);
            for(Map<String, Object> dataGroup : dateGroupList){
                List<Map<String, Object>> playList = new ArrayList();
                String data = (String)dataGroup.get("createTime");
                Map<String, Object> dateGroup21 = baseMapper.getdateGroup21(data);
                playList.add(dateGroup21);
                Map<String, Object> dateGroup24 = baseMapper.getdateGroup24(data);
                playList.add(dateGroup24);
                List<Map<String, Object>> dateGroup10 = baseMapper.getdateGroup10(data);
                playList.addAll(dateGroup10);
                dataGroup.put("playList",playList);
            }
            dateList.addAll(dateGroupList);
        }else if(gameType==21){
            //竞猜足球
            final BigDecimal winRate21 = baseMapper.productPerformanceTotal(startTime,endTime,gameType);
            list.add(new HashMap<String,Object>(){{put("type","21");put("winRate",winRate21);}});
            List<Map<String, Object>> dateGroupList = baseMapper.getDataRepresent(startTime,endTime,gameType);
            dateList.addAll(dateGroupList);
        }else if(gameType==24){
            //竞猜足球
            final BigDecimal winRate24 = baseMapper.productPerformanceTotal(startTime,endTime,gameType);
            list.add(new HashMap<String,Object>(){{put("type","24");put("winRate",winRate24);}});
            List<Map<String, Object>> dateGroupList = baseMapper.getDataRepresent(startTime,endTime,gameType);
            dateList.addAll(dateGroupList);
        }else{
            //电竞
            List<Map<String, Object>> listMap = baseMapper.productPerformanceTotal10(startTime,endTime,gameType);
            list.addAll(listMap);
            List<Map<String, Object>> dateGroupList = baseMapper.getDataRepresent10(startTime,endTime,gameType);
            dateList.addAll(dateGroupList);
        }
        dataMap.put("totalDataList",list);
        dataMap.put("dataRepresent",dateList);
        return dataMap;
    }
}
